package quq.missq.fragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.adapter.IJoinCommentAdapter;
import quq.missq.beans.IJoinBean;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 7/24/15.
 */
public class IJoinCommentFragment extends BaseFragment implements VolleyTool.HTTPListener,
        SwipeRefreshLayout.OnRefreshListener{

    private static final String TAG = "IJoinCommentFragment";
    private RelativeLayout mFragmentLayout;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private IJoinCommentAdapter adapter;
    private SwipeRefreshLayout mRefreshLayout;
    private View mFootView;

    private int maxResults = 10;
    private int currentPage = 1;
    private LinkedList<IJoinBean.Result> results = null;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        mFragmentLayout = (RelativeLayout)inflater.inflate(R.layout.i_join_fragment, container,false);
        currentPage = 1;
        initView();
        initData();
        initListener();
        loadNetworkData();
        return mFragmentLayout;
    }

    private FrameLayout fl_empty;
    private TextView tv_content1;
    private TextView tv_content2;

    private void initView(){
        mRefreshLayout = (SwipeRefreshLayout)mFragmentLayout.findViewById(R.id.refreshLayout);
        mRecyclerView = (RecyclerView)mFragmentLayout.findViewById(R.id.recyclerView);
        mLinearLayoutManager =  new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
    }

    private void initData(){
        results = new LinkedList<>();
        adapter = new IJoinCommentAdapter(getActivity());
        adapter.setData(results);
        mRecyclerView.setAdapter(adapter);
        mRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);

        fl_empty = (FrameLayout) mFragmentLayout.findViewById(R.id.fl_empty);
        tv_content1 = (TextView) mFragmentLayout.findViewById(R.id.tv_content1);
        tv_content2 = (TextView) mFragmentLayout.findViewById(R.id.tv_content2);
    }

    private void loadNetworkData(){
        Map<String,String> params = new HashMap<>();
        params.put("box","1");
        params.put("pn",currentPage+"");
        params.put("maxResults",maxResults+"");
        params.put("acc_token", UserTools.getUser(getActivity()).getAcc_token());
        VolleyTool.get(this.getActivity(),Constants.MY_ACTORS_URL, params, this, Constants.MY_ACTORS, IJoinBean.class);
    }

    private void initListener(){
        mRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && mLinearLayoutManager.findLastVisibleItemPosition()+1 == adapter.getItemCount()) {
                    operateFootView(true);
                    currentPage = currentPage+1;
                    loadNetworkData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    private void operateFootView(boolean isDisplay){
        if(mFootView == null){
            mFootView = mRecyclerView.findViewWithTag("footView");
        }
        if(isDisplay){
            mFootView.setVisibility(View.VISIBLE);
        }else {
            mFootView.setVisibility(View.GONE);
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        fl_empty.setVisibility(View.GONE);
        IJoinBean bsBean = (IJoinBean)t;
        if(bsBean.getCode() >= 0){
            LinkedList<IJoinBean.Result> beans = bsBean.getData().getResults();
            if(currentPage == 1){
                if(mRefreshLayout.isShown()){
                   mRefreshLayout.setRefreshing(false);
                }
                adapter.getData().clear();
                adapter.getData().addAll(0, beans);
                if (beans.size()==0){
                    fl_empty.setVisibility(View.VISIBLE);
                    tv_content1.setText("你还没有发表任何评论哦!");
                }
                adapter.notifyDataSetChanged();
            }else{
                if(beans.size() > 0){
                   adapter.getData().addAll(beans);
                   adapter.notifyDataSetChanged();
                }else{
                    if(results.size() > 10){
                        showToast("已加载完所有");
                    }
                }
                operateFootView(false);
            }
        }else{
            showToast(bsBean.getMessage());
       }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }

    @Override
    public void onRefresh() {
        fl_empty.setVisibility(View.GONE);
        currentPage = 1;
        loadNetworkData();
        mRefreshLayout.setRefreshing(false);
    }

}
